cmake_policy(SET CMP0140 NEW)

include ("${RunCMake_SOURCE_DIR}/check_errors.cmake")
unset (errors)

###############################################
## First test with a path defining all elements
###############################################
if (WIN32)
  set (path "C:/aa/bb/cc.ext1.ext2")
else()
  set (path "/aa/bb/cc.ext1.ext2")
endif()

cmake_path(GET path ROOT_NAME reference)
if (WIN32)
  set(output "$<PATH:GET_ROOT_NAME,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_ROOT_NAME,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_NAME returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_DIRECTORY reference)
if (WIN32)
  set(output "$<PATH:GET_ROOT_DIRECTORY,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_ROOT_DIRECTORY,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_DIRECTORY returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_PATH reference)
if (WIN32)
  set(output "$<PATH:GET_ROOT_PATH,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_ROOT_PATH,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_PATH returns bad data: ${output}")
endif()

cmake_path(GET path FILENAME reference)
if (WIN32)
  set(output "$<PATH:GET_FILENAME,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_FILENAME,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

cmake_path(GET path EXTENSION reference)
if (WIN32)
  set(output "$<PATH:GET_EXTENSION,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_EXTENSION,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()
cmake_path(GET path EXTENSION LAST_ONLY reference)
if (WIN32)
  set(output "$<PATH:GET_EXTENSION,LAST_ONLY,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_EXTENSION,LAST_ONLY,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION LAST_ONLY returns bad data: ${output}")
endif()

cmake_path(GET path STEM reference)
if (WIN32)
  set(output "$<PATH:GET_STEM,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_STEM,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()
cmake_path(GET path STEM LAST_ONLY reference)
if (WIN32)
  set(output "$<PATH:GET_STEM,LAST_ONLY,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_STEM,LAST_ONLY,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM LAST_ONLY returns bad data: ${reference}")
endif()

cmake_path(GET path RELATIVE_PART reference)
if (WIN32)
  set(output "$<PATH:GET_RELATIVE_PART,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_RELATIVE_PART,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "RELATIVE_PART returns bad data: ${output}")
endif()

cmake_path(GET path PARENT_PATH reference)
if (WIN32)
  set(output "$<PATH:GET_PARENT_PATH,C:/aa/bb/cc.ext1.ext2>")
else()
  set (output "$<PATH:GET_PARENT_PATH,/aa/bb/cc.ext1.ext2>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "PARENT_PATH returns bad data: ${output}")
endif()

######################################
## second, tests with missing elements
######################################
set (path "aa/bb/")

cmake_path(GET path ROOT_NAME reference)
set(output "$<PATH:GET_ROOT_NAME,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_NAME returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_DIRECTORY reference)
set(output "$<PATH:GET_ROOT_DIRECTORY,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_DIRECTORY returns bad data: >${output}<, >${reference}<")
endif()

cmake_path(GET path ROOT_PATH reference)
set(output "$<PATH:GET_ROOT_PATH,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_PATH returns bad data: ${output}")
endif()

cmake_path(GET path FILENAME reference)
set(output "$<PATH:GET_FILENAME,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

cmake_path(GET path EXTENSION reference)
set(output "$<PATH:GET_EXTENSION,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()

cmake_path(GET path STEM reference)
set(output "$<PATH:GET_STEM,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()

cmake_path(GET path RELATIVE_PART reference)
set(output "$<PATH:GET_RELATIVE_PART,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "RELATIVE_PART returns bad data: ${output}")
endif()

cmake_path(GET path PARENT_PATH reference)
set(output "$<PATH:GET_PARENT_PATH,aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "PARENT_PATH returns bad data: ${output}")
endif()

##################################
set (path "/aa/bb/")

cmake_path(GET path ROOT_NAME reference)
set(output "$<PATH:GET_ROOT_NAME,/aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_NAME returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_DIRECTORY reference)
set(output "$<PATH:GET_ROOT_DIRECTORY,/aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_DIRECTORY returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_PATH reference)
set(output "$<PATH:GET_ROOT_PATH,/aa/bb/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_PATH returns bad data: ${output}")
endif()

###################################
set (path "/")

cmake_path(GET path ROOT_NAME reference)
set(output "$<PATH:GET_ROOT_NAME,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_NAME returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_DIRECTORY reference)
set(output "$<PATH:GET_ROOT_DIRECTORY,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_DIRECTORY returns bad data: ${output}")
endif()

cmake_path(GET path ROOT_PATH reference)
set(output "$<PATH:GET_ROOT_PATH,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_PATH returns bad data: ${output}")
endif()

cmake_path(GET path FILENAME reference)
set(output "$<PATH:GET_FILENAME,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

cmake_path(GET path EXTENSION reference)
set(output "$<PATH:GET_EXTENSION,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()

cmake_path(GET path STEM reference)
set(output "$<PATH:GET_STEM,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()

cmake_path(GET path RELATIVE_PART reference)
set(output "$<PATH:GET_RELATIVE_PART,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "RELATIVE_PART returns bad data: ${output}")
endif()

cmake_path(GET path PARENT_PATH reference)
set(output "$<PATH:GET_PARENT_PATH,/>")
if (NOT output STREQUAL reference)
  list (APPEND errors "PARENT_PATH returns bad data: ${output}")
endif()

###################################
set (path ".file")

cmake_path(GET path FILENAME reference)
set(output "$<PATH:GET_FILENAME,.file>")
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

cmake_path(GET path EXTENSION reference)
set(output "$<PATH:GET_EXTENSION,.file>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()

cmake_path(GET path STEM reference)
set(output "$<PATH:GET_STEM,.file>")
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()

###################################
set (path ".file.ext")

cmake_path(GET path FILENAME reference)
set(output "$<PATH:GET_FILENAME,.file.ext>")
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

cmake_path(GET path EXTENSION reference)
set(output "$<PATH:GET_EXTENSION,.file.ext>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()
cmake_path(GET path EXTENSION LAST_ONLY reference)
set(output "$<PATH:GET_EXTENSION,LAST_ONLY,.file.ext>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()

cmake_path(GET path STEM reference)
set(output "$<PATH:GET_STEM,.file.ext>")
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()

###################################
set (path ".file.ext1.ext2")

cmake_path(GET path FILENAME reference)
set(output "$<PATH:GET_FILENAME,.file.ext1.ext2>")
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

cmake_path(GET path EXTENSION reference)
set(output "$<PATH:GET_EXTENSION,.file.ext1.ext2>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()
cmake_path(GET path EXTENSION LAST_ONLY reference)
set(output "$<PATH:GET_EXTENSION,LAST_ONLY,.file.ext1.ext2>")
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()

cmake_path(GET path STEM reference)
set(output "$<PATH:GET_STEM,.file.ext1.ext2>")
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()


######################################
## third, tests with list of paths
######################################
if (WIN32)
  set (paths "C:/aa/bb/cc.ext1.ext2" "D:/xx/yy/zz.ext3.ext4")
else()
  set (paths "/aa/bb/cc.ext1.ext2" "/xx/yy/zz.ext3.ext4")
endif()

function (compute_reference action)
  unset(reference)
  foreach (path IN LISTS paths)
    cmake_path(GET path ${ARGV} result)
    list(APPEND reference "${result}")
  endforeach()
  if (reference STREQUAL "")
    # define the list as 2 empty elements
    set(reference ";")
  endif()

  return(PROPAGATE reference)
endfunction()

compute_reference(ROOT_NAME)
if (WIN32)
  set(output "$<PATH:GET_ROOT_NAME,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_ROOT_NAME,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_NAME returns bad data: ${output}")
endif()

compute_reference(ROOT_DIRECTORY)
if (WIN32)
  set(output "$<PATH:GET_ROOT_DIRECTORY,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_ROOT_DIRECTORY,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_DIRECTORY returns bad data: ${output}")
endif()

compute_reference(ROOT_PATH)
if (WIN32)
  set(output "$<PATH:GET_ROOT_PATH,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_ROOT_PATH,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "ROOT_PATH returns bad data: ${output}")
endif()

compute_reference(FILENAME)
if (WIN32)
  set(output "$<PATH:GET_FILENAME,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_FILENAME,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "FILENAME returns bad data: ${output}")
endif()

compute_reference(EXTENSION)
if (WIN32)
  set(output "$<PATH:GET_EXTENSION,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_EXTENSION,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION returns bad data: ${output}")
endif()
compute_reference(EXTENSION LAST_ONLY)
if (WIN32)
  set(output "$<PATH:GET_EXTENSION,LAST_ONLY,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_EXTENSION,LAST_ONLY,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "EXTENSION LAST_ONLY returns bad data: ${output}")
endif()

compute_reference(STEM)
if (WIN32)
  set(output "$<PATH:GET_STEM,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_STEM,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM returns bad data: ${output}")
endif()
compute_reference(STEM LAST_ONLY)
if (WIN32)
  set(output "$<PATH:GET_STEM,LAST_ONLY,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_STEM,LAST_ONLY,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "STEM LAST_ONLY returns bad data: ${reference}")
endif()

compute_reference(RELATIVE_PART)
if (WIN32)
  set(output "$<PATH:GET_RELATIVE_PART,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_RELATIVE_PART,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "RELATIVE_PART returns bad data: ${output}")
endif()

compute_reference(PARENT_PATH)
if (WIN32)
  set(output "$<PATH:GET_PARENT_PATH,C:/aa/bb/cc.ext1.ext2;D:/xx/yy/zz.ext3.ext4>")
else()
  set (output "$<PATH:GET_PARENT_PATH,/aa/bb/cc.ext1.ext2;/xx/yy/zz.ext3.ext4>")
endif()
if (NOT output STREQUAL reference)
  list (APPEND errors "PARENT_PATH returns bad data: ${output}")
endif()


check_errors("PATH:GET..." ${errors})
